<template>
  <div>
    <!-- 搜索栏 -->
    <el-input
        v-model="searchText"
        placeholder="姓名"
        class="search-input"
        clearable
    />
    <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>

    <!-- 表格 -->
    <el-table :data="tableData" stripe style="width: 100%" v-loading="loading">
      <el-table-column prop="studentId" label="学号" sortable></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="class" label="所属班级"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="status" label="审核状态">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === '待审核' ? 'warning' : scope.row.status === '通过' ? 'success' : 'danger'">
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="handleApprove(scope.row)" :disabled="scope.row.status !== '待审核'">通过</el-button>
          <el-button size="mini" type="danger" @click="handleReject(scope.row)" :disabled="scope.row.status !== '待审核'">拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
        background
        layout="total, sizes, prev, pager, next"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchText: '',
      loading: false,
      tableData: [], // 表格数据
      total: 0, // 数据总条数
      pageSize: 10, // 每页条数
      currentPage: 1, // 当前页码
    };
  },
  methods: {
    // 搜索
    handleSearch() {
      this.fetchTableData();
    },

    // 通过审核
    handleApprove(row) {
      row.status = '通过';
      this.$message({
        type: 'success',
        message: `已通过 ${row.name} 的审核`
      });
    },

    // 拒绝审核
    handleReject(row) {
      row.status = '拒绝';
      this.$message({
        type: 'warning',
        message: `已拒绝 ${row.name} 的审核`
      });
    },

    // 获取表格数据
    fetchTableData() {
      this.loading = true;
      // 模拟请求数据
      setTimeout(() => {
        // 模拟数据
        this.tableData = [
          { studentId: '20210001', name: '张三', class: '一班', email: 'zhangsan@example.com', status: '待审核' },
          { studentId: '20210002', name: '李四', class: '二班', email: 'lisi@example.com', status: '通过' },
          { studentId: '20210003', name: '王五', class: '三班', email: 'wangwu@example.com', status: '待审核' },
          { studentId: '20210004', name: '赵六', class: '一班', email: 'zhaoliu@example.com', status: '拒绝' },
        ];
        this.total = this.tableData.length;
        this.loading = false;
      }, 1000);
    },

    // 每页条数改变
    handleSizeChange(size) {
      this.pageSize = size;
      this.fetchTableData();
    },

    // 当前页码改变
    handleCurrentChange(page) {
      this.currentPage = page;
      this.fetchTableData();
    },
  },
  mounted() {
    // 初次加载数据
    this.fetchTableData();
  },
};
</script>

<style scoped>
.search-input {
  width: 200px;
  margin-right: 10px;
}
</style>
